<template>
	<div>
		<el-card shadow="hover" header="操作">
			<el-form :inline="true" 
					ref="ruleFormRef"
					:model="ruleForm"
					:rules="rules">
				<el-form-item prop="date">
					<el-date-picker
						v-model="ruleForm.date"
						type="month"
						placeholder="选择清算月份"
					/>
				</el-form-item>
				<el-form-item>
					<el-button @click="readLiqData(ruleFormRef)" size="default" type="primary">
						<SvgIcon name="iconfont icon-step" />
						查询
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="clear(ruleFormRef)" size="default" type="danger">
						<SvgIcon name="ele-RefreshRight" />
						重置
					</el-button>
				</el-form-item>
				<el-form-item>
					<el-switch
						v-model="ruleForm.tcq"
						size="large"
						inline-prompt
						width="80px"
						style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
						inactive-text="区直"
						active-text="南宁市"
					/>
				</el-form-item>
				
			</el-form>
		</el-card>
		<el-card shadow="hover" header="清算申请详情">
			<el-table :data="tableData" :border="parentBorder" style="width: 100%">
				<el-table-column type="expand">
					<template #default="props">
						<el-descriptions title="清算信息详情" :column="2" :border="true">
							<el-descriptions-item label="清算申请状态" label-align="right" align="center" width="250px"
							><b><el-tag effect="dark" size="small">{{ props.row.clrStas }}</el-tag></b></el-descriptions-item>
							<el-descriptions-item label="审核备注" label-align="right" align="center">{{ props.row.dfrMemo }}</el-descriptions-item>
							<el-descriptions-item label="清算流水号" label-align="right" align="center">{{ props.row.clrAppyEvtId }}</el-descriptions-item>
							<el-descriptions-item label="收款确认标志" label-align="right" align="center">{{ props.row.cnfmFlag }}</el-descriptions-item>
							<el-descriptions-item label="清算经办机构" label-align="right" align="center">{{ props.row.clrOptins }}</el-descriptions-item>
							<el-descriptions-item label="一级清算类别" label-align="right" align="center">{{ props.row.clrType }}</el-descriptions-item>
							<el-descriptions-item label="清算年月" label-align="right" align="center">{{ props.row.clrYm }}</el-descriptions-item>
							<el-descriptions-item label="清算人次" label-align="right" align="center">{{ props.row.clrPsntime }}</el-descriptions-item>
							<el-descriptions-item label="医保认可费用" label-align="right" align="center">{{ props.row.hiAgreSumfee }}</el-descriptions-item>
							<el-descriptions-item label="总费用" label-align="right" align="center">{{ props.row.medfeeSumamt }}</el-descriptions-item>
							<el-descriptions-item label="统筹基金合计" label-align="right" align="center">{{ props.row.hifpPay }}</el-descriptions-item>
							<el-descriptions-item label="居民二次报销" label-align="right" align="center">{{ props.row.rsdtSdryReim }}</el-descriptions-item>
							<el-descriptions-item label="个人账户支付" label-align="right" align="center">{{ props.row.acctPay }}</el-descriptions-item>
							<el-descriptions-item label="公补支出" label-align="right" align="center">{{ props.row.cvlservPay }}</el-descriptions-item>
							<el-descriptions-item label="职工大额支出" label-align="right" align="center">{{ props.row.hifobPay }}</el-descriptions-item>
							<el-descriptions-item label="其他基金支出" label-align="right" align="center">{{ props.row.othfundPay }}</el-descriptions-item>
							<el-descriptions-item label="生育基金支出" label-align="right" align="center">{{ props.row.matnFundPay }}</el-descriptions-item>
							<el-descriptions-item label="医疗救助基金" label-align="right" align="center">{{ props.row.mafPay }}</el-descriptions-item>
							<el-descriptions-item label="居民大病/职工大额补充" label-align="right" align="center">{{ props.row.hifmiPay }}</el-descriptions-item>
							<el-descriptions-item label="残军专项补助" label-align="right" align="center">{{ props.row.hifdmPay }}</el-descriptions-item>
							<el-descriptions-item label="居民政府兜底" label-align="right" align="center">{{ props.row.hifesPay }}</el-descriptions-item>
							<el-descriptions-item label="个人现金支出" label-align="right" align="center">{{ props.row.cashPayamt }}</el-descriptions-item>
							<el-descriptions-item label="创建人姓名" label-align="right" align="center">{{ props.row.crterName }}</el-descriptions-item>
							<el-descriptions-item label="创建时间" label-align="right" align="center">{{ props.row.crteTime }}</el-descriptions-item>
							<el-descriptions-item label="财务支付时间" label-align="right" align="center">{{ props.row.finAcenTime }}</el-descriptions-item>
						</el-descriptions>
					</template>
				</el-table-column>
				<el-table-column prop="clrStas" label="清算申请状态" />
				<el-table-column prop="cnfmFlag" label="收款确认标志" />
				<el-table-column prop="medfeeSumamt" label="总费用"/>
				<el-table-column prop="clrOptins" label="清算经办机构" />
				<el-table-column prop="clrType" label="一级清算类别"/>
				<el-table-column prop="clrYm" label="清算年月"/>
				<el-table-column fixed="right" label="操作" width="180">
					<template #default="scope">
						<el-button link type="primary" size="small" @click="liquApply(scope.$index)">清算</el-button>
						<el-button link type="primary" size="small" @click="liquCancel(scope.$index)">撤销</el-button>
						<el-button link type="primary" size="small" @click="confirmApply(scope.$index)">收款确认</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, defineComponent, ref } from 'vue';
import post from '/@/utils/post';
import clrStas from '/@/config/yinhai/clrStas';
import codeHandler from '/@/common/codeHandler';
import { ElMessage, FormRules, FormInstance } from 'element-plus';
import Big from 'big.js';
import common from '/@/common/common';
import { Session } from '/@/utils/storage';

export default defineComponent({
	name: 'customUpdate',
	setup() {
		const state = reactive({
			tableData: [],
			realData: [],
			ruleForm: {
				date: null,
				tcq: true,
				orgname: '',
				orgcode: '',
			},
			clrstas: clrStas.data,
			parentBorder: false,
			childBorder: false,
			date: '',
			rules: reactive<FormRules>({
				date: [
					{ required: true, message: '清算日期必填，不能留空', trigger: 'blur' },
				],
			})
		});
		const ruleFormRef = ref<FormInstance>()
		// 下一步点击
		const clear = (formEl: FormInstance | undefined) => {
			state.tableData = []
			if (!formEl) return
  			formEl.resetFields()
		}
		const readLiqData = (formEl: FormInstance | undefined) => {
			state.ruleForm.orgname = Session.get('drugUserInfo').userName
			state.ruleForm.orgcode = Session.get('drugUserInfo').chsCode
			if (!formEl) return
			formEl.validate((valid) => {
				if (!valid) return
				let tcqcode = getTCQcode()
				post.postUrlEncode('clinicFlag=0&clrYm='+ common.gmtToString(state.ruleForm.date, common.YYYYMM) +'&insuAdmdvsFlag=1','http://10.98.34.192:8979/api/neuyyfftool/fms2/web/liqHealthRoom/queryByPage?'+
				'name='+ state.ruleForm.orgname +
				'&orgCode='+ state.ruleForm.orgcode +
				'&poolAreaCodg='+ tcqcode +
				'&admDvs='+ tcqcode, {'Content-Type': 'application/x-www-form-urlencoded'}).then(res=>{
					if (returnError(res)) {
						return
					}
					state.realData = res.data.data.result
					for (let i = state.realData.length - 1; i >= 0; i--) {
						for (let j = i - 1; j >= 0; j--) {
							if ( 
								state.realData[j]['clrStas'] == state.realData[i]['clrStas'] &&
								state.realData[j]['clrOptins'] == state.realData[i]['clrOptins'] &&
								state.realData[j]['clrType'] == state.realData[i]['clrType'] &&
								state.realData[j]['clrAppyEvtId'] == state.realData[i]['clrAppyEvtId']
							) {
								//JavaScript做浮点运算还有异常，这个是需要注意得，得先变成整数，再除回来
								//中心返回得数字有的以字符串形式，得转换一下
								state.realData[j]['clrPsntime'] = new Big(Number(state.realData[j]['clrPsntime'])).plus(new Big(Number(state.realData[i]['clrPsntime'])))
								state.realData[j]['hiAgreSumfee'] = new Big(Number(state.realData[j]['hiAgreSumfee'])).plus(new Big(Number(state.realData[i]['hiAgreSumfee'])))
								state.realData[j]['medfeeSumamt'] = new Big(Number(state.realData[j]['medfeeSumamt'])).plus(new Big(Number(state.realData[i]['medfeeSumamt'])))
								state.realData[j]['hifpPay'] = new Big(Number(state.realData[j]['hifpPay'])).plus(new Big(Number(state.realData[i]['hifpPay'])))
								state.realData[j]['rsdtSdryReim'] = new Big(Number(state.realData[j]['rsdtSdryReim'])).plus(new Big(Number(state.realData[i]['rsdtSdryReim'])))
								state.realData[j]['acctPay'] = new Big(Number(state.realData[j]['acctPay'])).plus(new Big(Number(state.realData[i]['acctPay'])))
								state.realData[j]['cvlservPay'] = new Big(Number(state.realData[j]['cvlservPay'])).plus(new Big(Number(state.realData[i]['cvlservPay'])))
								state.realData[j]['hifobPay'] = new Big(Number(state.realData[j]['hifobPay'])).plus(new Big(Number(state.realData[i]['hifobPay'])))
								state.realData[j]['othfundPay'] = new Big(Number(state.realData[j]['othfundPay'])).plus(new Big(Number(state.realData[i]['othfundPay'])))
								state.realData[j]['matnFundPay'] = new Big(Number(state.realData[j]['matnFundPay'])).plus(new Big(Number(state.realData[i]['matnFundPay'])))
								state.realData[j]['mafPay'] = new Big(Number(state.realData[j]['mafPay'])).plus(new Big(Number(state.realData[i]['mafPay'])))
								state.realData[j]['hifmiPay'] = new Big(Number(state.realData[j]['hifmiPay'])).plus(new Big(Number(state.realData[i]['hifmiPay'])))
								state.realData[j]['hifdmPay'] = new Big(Number(state.realData[j]['hifdmPay'])).plus(new Big(Number(state.realData[i]['hifdmPay'])))
								state.realData[j]['hifesPay'] = new Big(Number(state.realData[j]['hifesPay'])).plus(new Big(Number(state.realData[i]['hifesPay'])))
								state.realData[j]['cashPayamt'] = new Big(Number(state.realData[j]['cashPayamt'])).plus(new Big(Number(state.realData[i]['cashPayamt'])))
								state.realData.splice(i, 1)
								break
							}
						}
						
					}
					state.tableData = codeHandler.transJsonCodeToName(state.realData, 'clrStas', codeHandler.CLR_STAS)
					state.tableData = codeHandler.transJsonCodeToName(state.tableData, 'clrOptins', codeHandler.AREA)
					state.tableData = codeHandler.transJsonCodeToName(state.tableData, 'clrType', codeHandler.CLR_TYPE)
					state.tableData = codeHandler.transJsonCodeToName(state.tableData, 'cnfmFlag', codeHandler.CNFM_FLAG)
				})
			})
		}
		const liquApply = (index: number) => {
			let input ={"clrType": state.realData[index]['clrType'],
			"clrYm": state.realData[index]['clrYm'],
			"clinicFlag": "0",
			"insuAdmdvsFlag": "1",
			"row": {
				"clrAppyEvtId": state.realData[index]['clrAppyEvtId'],
				"clrStas": state.realData[index]['clrStas'],
				"dfrMemo": state.realData[index]['dfrMemo'],
				"cnfmFlag": state.realData[index]['cnfmFlag'],
				"clrOptinsClrType": state.realData[index]['clrOptins']+state.realData[index]['clrType'],
				"clrType": state.realData[index]['clrType'],
				"clrOptins": state.realData[index]['clrOptins'],
				"clrYm": state.realData[index]['clrYm'],
				"clrPsntime": state.realData[index]['clrPsntime'],
				"hiAgreSumfee": state.realData[index]['hiAgreSumfee'],
				"medfeeSumamt": state.realData[index]['medfeeSumamt'],
				"hifpPay": state.realData[index]['hifpPay'],
				"rsdtSdryReim": state.realData[index]['rsdtSdryReim'],
				"acctPay": state.realData[index]['acctPay'],
				"cvlservPay": state.realData[index]['cvlservPay'],
				"hifobPay": state.realData[index]['hifobPay'],
				"othfundPay": state.realData[index]['othfundPay'],
				"matnFundPay": state.realData[index]['matnFundPay'],
				"mafPay": state.realData[index]['mafPay'],
				"hifmiPay": state.realData[index]['hifmiPay'],
				"hifdmPay": state.realData[index]['hifdmPay'],
				"hifesPay": state.realData[index]['hifesPay'],
				"cashPayamt": state.realData[index]['cashPayamt'],
				"crterName": state.ruleForm.orgname,
				"crteTime": common.gmtToString(new Date(), common.YYYY_MM_DDHH24MISS),
				"finAcenTime": null
				}
			}
			post.postList(input, 'http://10.98.34.192:8979/api/neuyyfftool/fms2/web/liqHealthRoom/commitClr?'+
				'name='+ state.ruleForm.orgname +
				'&orgCode='+ state.ruleForm.orgcode +
				'&poolAreaCodg='+ state.realData[index]['clrOptins'] +
				'&admDvs='+ state.realData[index]['clrOptins']).then(res=>{
				if (returnError(res)) {
					return
				}
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '清算申请：' + res.data.message,
					type: 'success',
				})
			})
		}
		const liquCancel = (index: number) => {
			if(common.objectUnavailable(state.realData[index]['clrAppyEvtId'])){
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '没有申请过清算不能撤销，或者说已经申请清算的，请再次查询清算结果，再操作撤销',
					type: 'error',
				})
				return
			}
			post.postList({}, 'http://10.98.34.192:8979/api/neuyyfftool/fms2/web/liqHealthRoom/cancelClr?'+
				'name='+ state.ruleForm.orgname +
				'&orgCode='+ state.ruleForm.orgcode +
				'&poolAreaCodg='+ state.realData[index]['clrOptins'] +
				'&admDvs='+ state.realData[index]['clrOptins'] +
				'&clrAppyEvtId='+ state.realData[index]['clrAppyEvtId'] +
				'&clinicFlag=0' +
				'&clrYm='+ state.realData[index]['clrYm']).then(res=>{
				if (returnError(res)) {
					return
				}
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '申请撤销：' + res.data.message,
					type: 'success',
				})
				
			})
		}

		const confirmApply = (index: number) => {
			let param = 'http://10.98.34.192:8979/api/neuyyfftool/fms2/web/liqHealthRoom/updatePayConfirmStats?'+
				'name='+ state.ruleForm.orgname +
				'&orgCode='+ state.ruleForm.orgcode +
				'&poolAreaCodg='+ state.realData[index]['clrOptins'] +
				'&admDvs='+ state.realData[index]['insuAdmdvs']+
				'&clrOptins='+ state.realData[index]['clrOptins'] +
				'&clrType='+ state.realData[index]['clrType'] +
				'&clrYm='+ state.realData[index]['clrYm'] +
				'&clrAppyEvtId='+ state.realData[index]['clrAppyEvtId'] +
				'&drgFlag=false' +
				'&clrYm='+ state.realData[index]['clrYm']
			post.postList({}, param).then(res=>{
				if (returnError(res)) {
					return
				}
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '收款确认：' + res.data.message,
					type: 'success',
				})
			})
		}

		const getTCQcode = () => {
			if(state.ruleForm.tcq){
				return '450199'
			} else {
				return '459900'
			}
		}

		const returnError = (res) => {
			if(res.status != 200){
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '请求失败，错误码：' + res,
					type: 'error',
				})
				return true
			} 
			if(res.data.code != 0) {
				ElMessage({
					showClose: true,
					duration: 5000,
					message: '服务器返回错误：' + res.data.message,
					type: 'error',
				})
				return true
			}
			return false
		}
		return {
			readLiqData,
			liquApply,
			liquCancel,
			confirmApply,
			clear,
			ruleFormRef,
			...toRefs(state),
		};
	}
})
</script>
